package com.haliyoo.adhere.activity;

import java.util.List;

import com.haliyoo.adhere.R;
import com.haliyoo.adhere.bean.WeiboBean;
import com.haliyoo.adhere.sqlite.DataHelper4Weibo;
import com.haliyoo.adhere.widget.LocalWeiboLayout;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class DraftsActivity extends Activity {
	private static String TAG = "DraftsActivity";

	/**
	 * 内部类，实现微博数据获取
	 * @author wation.haliyoo
	 *
	 */
	class WeiboList extends LocalWeiboLayout{

		WeiboList(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}
		
		/// 删除数据库中所有项
		void deleteAllWeibo() {

	    	DataHelper4Weibo dbHelper = null;
	    	
	    	Log.i(TAG, "entry deleteAllDrafts function.");
	    	
	        //获取账号列表
	        dbHelper = new DataHelper4Weibo(DraftsActivity.this);
	        
	        for (WeiboBean weiboBean : getWeiboList()) {
	        	dbHelper.delete(Integer.parseInt(weiboBean.getId()));
	        }

	        //刷新页面
	    	refreshWeiboList();
		}

		@Override
		public List<WeiboBean> onGetWeiboList() {
			// TODO Auto-generated method stub
			if (LoginActivity.getUser() == null) {
				Toast.makeText(DraftsActivity.this, 
						R.string.STR_NONE_USER, 
						Toast.LENGTH_SHORT).show();
			} else {
				List<WeiboBean> wbList = null;

		    	DataHelper4Weibo dbHelper = null;
		    	
		        //获取账号列表
		        dbHelper = new DataHelper4Weibo(DraftsActivity.this);
		        wbList = dbHelper.select();
				
				return wbList;
			}
			
			return null;
		}

		@Override
		public void onShowWeiboDetail(AdapterView<?> arg0, View view, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			WeiboBean weiboBean = (WeiboBean) view.getTag();
			if (weiboBean != null) {
				Intent intent = new Intent(DraftsActivity.this,
						CreativeActivity.class);
				Bundle b = new Bundle();
				b.putString("id", weiboBean.getId());
				intent.putExtras(b);
				DraftsActivity.this.startActivity(intent);
			}
		}

		@Override
		public void onShowWeiboDetail(WeiboBean weiboBean) {
			// TODO Auto-generated method stub

			if (weiboBean != null) {
				Intent intent = new Intent(DraftsActivity.this,
						CreativeDesignActivity.class);
				
				Log.i(TAG, "weiboid: " + weiboBean.getId());
				
				intent.putExtra("id", weiboBean.getId());
				DraftsActivity.this.startActivity(intent);
			}
		}

		@Override
		public void onFinishedPublish(WeiboBean weiboBean) {
			// TODO Auto-generated method stub

	    	DataHelper4Weibo dbHelper = null;
	    	
	    	Log.i(TAG, "entry onFinishedPublish function.");
	    	
	        //获取账号列表
	        dbHelper = new DataHelper4Weibo(DraftsActivity.this);
	        dbHelper.delete(Integer.parseInt(weiboBean.getId()));
	        
	        //刷新页面
	        refreshWeiboList();
		}
	}
	
	private WeiboList weiboList = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
    	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.drafts);
		
		/// 加载微博列表
		if (weiboList == null) {
			weiboList = new WeiboList(DraftsActivity.this);
		}
		weiboList.refreshWeiboList();
		
		/**
		 * 返回键处理
		 */
		ImageButton ibBack = (ImageButton) findViewById(R.id.IMAGE_BUTTON_BACK);
		ibBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();  
			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		/**
		 * 删除草稿箱
		 */
		case 0:
			weiboList.deleteAllWeibo();
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, 0, 0, getString(R.string.STR_DELETE_ALL)).setIcon(R.drawable.delete_all);
		return super.onCreateOptionsMenu(menu);
	}
}
